﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmKetQuaHocTap : DevExpress.XtraEditors.XtraForm
    {
        private frmMain frmM;
        private DataTable dtcd;
        public frmKetQuaHocTap(frmMain frm)
        {
            InitializeComponent();
            frmM = frm;
            loadYear();
        }

        public void loadYear()
        {
            for (int i = 2000; i < 2030; i++)
                this.comboBoxNamHoc.Properties.Items.Add(i.ToString() + "-" + (i + 1).ToString());
        }

        private void comboBoxNamHoc_SelectedValueChanged(object sender, EventArgs e)
        {
            tblchidoan cd = new tblchidoan();
            dtcd = new DataTable();
            dtcd = cd.SelectCDTheoNamHoc(this.comboBoxNamHoc.Text);
                        foreach (DataRow r in dtcd.Rows)
            {
                this.comboBoxChiDoan.Properties.Items.Add(r["khoi"].ToString()+r["lop"].ToString());
            }
        }

        private void btnLietKe_Click(object sender, EventArgs e)
        {
            int hocky = 1;
            if (this.checkEditHocKy2.Checked)
                hocky = 2;
            int chidoan = 0;
            string namhoc = this.comboBoxNamHoc.Text;
            chidoan = int.Parse(this.dtcd.Rows[this.comboBoxChiDoan.SelectedIndex]["chidoan_id"].ToString());
            tblDoanvien_ketquahoctap kq = new tblDoanvien_ketquahoctap();
            DataTable dt = new DataTable();
            dt = kq.SelectDV(hocky, namhoc);
            dt.Columns.Add("stt", typeof(int));
            dt.Columns.Add("diem", typeof(int));
            int i = 0;
            foreach (DataRow r in dt.Rows)
            {
                i++;
                r["stt"] = i;
            }
            this.gridControlKQHT.DataSource = dt;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void frmKetQuaHocTap_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmM.frmKQHT_isShown = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                tblDoanvien_ketquahoctap kq = new tblDoanvien_ketquahoctap();
                int id = 0;
                int hocky = 1;
                if (this.checkEditHocKy2.Checked)
                    hocky = 2;
                int chidoan = int.Parse(this.dtcd.Rows[this.comboBoxChiDoan.SelectedIndex]["chidoan_id"].ToString());
                string namhoc = this.comboBoxNamHoc.Text;
                int diem = 0;
                for (int i = 0; i < this.gridViewHSDV.RowCount; i++)
                {
                    id = int.Parse(this.gridViewHSDV.GetRowCellValue(i, "id").ToString());
                    diem = int.Parse(this.gridViewHSDV.GetRowCellValue(i, "diem").ToString());
                    kq.ketquahoctap(id, hocky, namhoc, diem);
                }
                DevExpress.XtraEditors.XtraMessageBox.Show("Lưu kết quả học tập thành công!");
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error:" + ex.Message);
            }
        }
    }
}